/**
 * @Title: SplashActivity.java
 * @Package com.unicom.eoms.activity
 * @Description: TODO  启动页
 * @author li_xiaoyue
 * @version 2013-4-2
 */
package com.hktf.moedokilock.activity;

import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hktf.corelib.constant.CommonConstant;
import com.hktf.corelib.constant.KEYS;
import com.hktf.corelib.core.ActivityManager;
import com.hktf.corelib.utils.CommUtils;
import com.hktf.corelib.utils.Config;
import com.hktf.corelib.utils.FileUtil;
import com.hktf.corelib.utils.LogMds;
import com.hktf.corelib.utils.MMKVUtils;
import com.hktf.corelib.utils.SharePreUtils;
import com.hktf.corelib.utils.UIUtils;
import com.hktf.moedokilock.R;
import com.hktf.uilib.activity.ProtocolWebViewActivity;
import com.hktf.uilib.app.BaseActivity;
import com.hktf.uilib.app.UiApplication;
import com.hktf.uilib.utils.CheckWordUtils;
import com.hktf.uilib.utils.DlgUtils;
import com.hktf.uilib.widget.RequestPermissionDialog;
import com.permissionx.guolindev.PermissionX;
import com.permissionx.guolindev.callback.ExplainReasonCallbackWithBeforeParam;
import com.permissionx.guolindev.callback.ForwardToSettingsCallback;
import com.permissionx.guolindev.callback.RequestCallback;
import com.permissionx.guolindev.request.ExplainScope;
import com.permissionx.guolindev.request.ForwardScope;

import java.util.List;
import java.util.Locale;

/**
 * @author cdz
 * @ClassName: SplashActivity
 * @Description: TODO 启动页
 * @date 2025-3-2 上午9:15:07
 */
public class SplashActivity extends BaseActivity {

    private TextView text_version;

    private RelativeLayout splash_rl;


    @Override
    public int getLayoutResID() {
        return R.layout.ats_splash;
    }

    @Override
    public void initView() {
        text_version = (TextView) findViewById(R.id.tv_version_number);
        String versionNum = getVersionCode();
        text_version.setText(versionNum);
        splash_rl = (RelativeLayout) findViewById(R.id.splash_rl);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        // TODO: 2023/5/5 检测是否在root手机上使用 如果是退出app
        checkRoot();

        DisplayMetrics dm = getResources().getDisplayMetrics();
        int screenWidth = dm.widthPixels;
        int screenHeight = dm.heightPixels;
        LogMds.i("cdz","width="+screenWidth+"....height="+screenHeight);
        LogMds.i("cdz","width="+ UIUtils.px2dip(screenWidth) +"....height="+UIUtils.px2dip(screenHeight));




        //验证签名是否正确
        boolean checkPassed = new CheckWordUtils(SplashActivity.this).getCheckResult();

        if (!checkPassed && !Config.LOG_OFF_DEBUG) {
            ActivityManager.getScreenManager().popAllActivity();
            System.exit(0);
        } else {
            //            startAnim();

            String flag = SharePreUtils.getString(this,KEYS.IS_FIRST,"-1");
            //            flag=0;
            if (flag.equals("-1")) {
                show1();
            }else {
                startAnim();
            }
        }
    }

    private int flag;

    private void show1(){
        DlgUtils.showProtocol(this, new DlgUtils.PickListener() {
            @Override
            public void onPick(int index) {
                Intent intent = new Intent(SplashActivity.this, ProtocolWebViewActivity.class);
                switch (index) {
                    case 1:
                        intent.putExtra("flag", 1);
                        startActivity(intent);
                        flag=1;
                        break;
                    case 2:
                        intent.putExtra("flag", 0);
                        startActivity(intent);
                        flag=1;
                        break;
                    case 3:
                        show2();
                        break;
                    case 4:
                        startAnim();
                        break;
                }
            }

            @Override
            public void onCancel() {

            }
        });
    }
    private void show2(){
        DlgUtils.showProtocol2(this, new DlgUtils.PickListener() {
            @Override
            public void onPick(int index) {
                Intent intent = new Intent(SplashActivity.this, ProtocolWebViewActivity.class);
                switch (index) {
                    case 1:
                        intent.putExtra("flag", 1);
                        startActivity(intent);
                        flag=2;
                        break;
                    case 2:
                        intent.putExtra("flag", 0);
                        startActivity(intent);
                        flag=2;
                        break;
                    case 3:
                        finish();
                        break;
                    case 4:
                        startAnim();
                        break;
                }
            }

            @Override
            public void onCancel() {

            }
        });
    }


    private void jump() {
        if (CommUtils.isLogin()) {
            startActivity(new Intent(this, MainActivity.class));
            finish();
        } else {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }
    }


    private void startAnim() {
        UiApplication.initOtherSdk();

        MMKVUtils.putInteger(KEYS.IS_FIRST,1);
        SharePreUtils.setString(this,KEYS.IS_FIRST,"1");

        AnimationSet set = new AnimationSet(false);

        AlphaAnimation alpha = new AlphaAnimation(0, 1);
        alpha.setDuration(1000);// 动画时间
        alpha.setFillAfter(true);// 保持动画状态
        set.addAnimation(alpha);

        set.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            // 动画执行结束
            @Override
            public void onAnimationEnd(Animation animation) {
                //先申请权限
                PermissionX.init(SplashActivity.this)
                        .permissions(
//                                android.Manifest.permission.CAMERA,
//                                android.Manifest.permission.ACCESS_FINE_LOCATION,
//                                android.Manifest.permission.ACCESS_COARSE_LOCATION,
                                android.Manifest.permission.ACCESS_NETWORK_STATE,
                                android.Manifest.permission.READ_PHONE_STATE,
                                android.Manifest.permission.ACCESS_WIFI_STATE,
                                android.Manifest.permission.WRITE_EXTERNAL_STORAGE
                                //                                android.Manifest.permission.RECORD_AUDIO,
//                                android.Manifest.permission.READ_EXTERNAL_STORAGE,
//                                Manifest.permission.WRITE_EXTERNAL_STORAGE
                        )
                        .onExplainRequestReason(new ExplainReasonCallbackWithBeforeParam() {
                            @Override
                            public void onExplainReason(ExplainScope scope, List<String> deniedList, boolean beforeRequest) {
                                for (String str : deniedList) {
                                    LogMds.d("hxwPer", "str " + str);
                                }
//                                scope.showRequestReasonDialog(deniedList, "超级卡站需要您同意以下权限才能正常使用", "同意");
                                scope.showRequestReasonDialog(new RequestPermissionDialog(SplashActivity.this, "缺少权限",
                                        UIUtils.getString(R.string.app_name)+"需要您同意以下权限才能正常使用", deniedList));

                            }
                        })
                        .onForwardToSettings(new ForwardToSettingsCallback() {
                            @Override
                            public void onForwardToSettings(ForwardScope scope, List<String> deniedList) {
                                for (String str : deniedList) {
                                    LogMds.d("hxwPer", "str " + str);
                                }
//                                scope.showForwardToSettingsDialog(deniedList, "您需要去设置中手动开启以下权限", "确定");
                                scope.showForwardToSettingsDialog(new RequestPermissionDialog(SplashActivity.this, "缺少权限",
                                        "您需要去设置中手动开启权限", deniedList));
                            }
                        })
                        .request(new RequestCallback() {
                            @Override
                            public void onResult(boolean allGranted, List<String> grantedList, List<String> deniedList) {
                                if (allGranted) {
                                    jump();
                                } else {
                                    Toast.makeText(SplashActivity.this, "您拒绝了如下权限：" + deniedList, Toast.LENGTH_SHORT).show();
                                }
                            }
                        });

            }
        });
        splash_rl.startAnimation(set);

    }



    // hexunwu TODO 获取当前版本号
    private String getVersionCode() {
        String versionCode = "";
        try {
            // 获取软件版本号，对应AndroidManifest.xml下android:versionCode
            versionCode = FileUtil.getVersion(this.getPackageManager()
                    .getPackageInfo(this.getPackageName(), 0).versionName);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }


    @Override
    protected void onResume() {
        super.onResume();
//        JPushInterface.onResume(this);
        if (flag==1) {
            show1();
        }else if (flag==2) {
            show2();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    protected void onDestroy() {
        super.onDestroy();

    }

    protected void switchLanguage(String language) {
        // 设置应用语言类型
        Resources resources = getResources();
        Configuration config = resources.getConfiguration();
        DisplayMetrics dm = resources.getDisplayMetrics();
        if (language.equals(Locale.ENGLISH.toString())) {
            config.locale = Locale.ENGLISH;
        } else if (language.equals(Locale.TRADITIONAL_CHINESE.toString())) {
            config.locale = Locale.TRADITIONAL_CHINESE;

        } else {
            config.locale = Locale.SIMPLIFIED_CHINESE;
        }
        resources.updateConfiguration(config, dm);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return true;
    }
}
